package com.fishery.monitor.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.monitor.dao.EquipmentDao;
import com.fishery.monitor.mapper.MonitorEquipmentMapper;

import com.fishery.monitor.pojo.Equipment;
import com.fishery.monitor.pojo.MonitorBase;
import com.fishery.monitor.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

/**
 * 服务层
 *
 * @author Administrator
 *
 */
@Service
public class EquipmentService extends ServiceImpl<MonitorEquipmentMapper, Equipment> {

    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 根据基地id和typeId查询所有设备,分页
     */
    public IPage<Equipment> findAllByBaseId(String baseId, String typeId, int page, int size){

        Map whereMap = new HashMap();
        whereMap.put("baseId",baseId);
        whereMap.put("typeId",typeId);
        Page<Equipment> Page = new Page<>(page,size);;
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.allEq(whereMap,false);

        return equipmentDao.findAll(Page,wrapper);
    }
    /**
     * 查询全部列表
     * @return
     */
    public List<Equipment> findAll() {
        return this.list(null);
    }


    /**
     * 条件查询+分页
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public IPage<Equipment> findSearch(Map whereMap, int page, int size) {
        Page<Equipment> Page = new Page<>(page,size);
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.allEq(whereMap,false);
        return equipmentDao.findAll(Page,wrapper);
    }


    /**
     * 条件查询
     * @param whereMap
     * @return
     */
    public List<Equipment> findSearch(Map whereMap) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.allEq(whereMap,false);
        return equipmentDao.findAll(wrapper);
    }

    /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    public Equipment findById(String id) {
        return this.findById(id);
    }

    /**
     * 增加
     * @param equipment
     */
    public void add(Equipment equipment) {
        equipment.setId( idWorker.nextId()+"" );
//        Date date = new Date();
//        Timestamp timestamp = new Timestamp(date.getTime());
//        equipment.setUpdateTime(timestamp);
        this.save(equipment);
    }


    /**
     * 修改
     * @param equipment
     */
    public void update(Equipment equipment) {
        this.save(equipment);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(String id) {
        this.removeById(id);
    }




}
